<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Utils
{
	/* PHP Array to JSON*/
	function php2js($a)
	{
	    if (is_null($a)) return 'null';
	    if ($a === false) return 'false';
	    if ($a === true) return 'true';
	    if (is_scalar($a)) {
	        $a = addslashes($a);
	        $a = str_replace("\n", '\n', $a);
	        $a = str_replace("\r", '\r', $a);
	        $a = preg_replace('{(</)(script)}i', "$1'+'$2", $a); // for FORM loader
	        return "'$a'";
	    }
	    $isList = true;
	    for ($i=0, reset($a); $i<count($a); $i++, next($a))
	        if (key($a) !== $i) { $isList = false; break; }
	    $result = array();
	    if ($isList) {
	        foreach ($a as $v) $result[] = $this->php2js($v);
	        return '[ ' . join(', ', $result) . ' ]';
	    } else {
	        foreach ($a as $k=>$v) $result[] = $this->php2js($k) . ': ' . $this->php2js($v);
	        return '{ ' . join(', ', $result) . ' }';
	    }
	}
	
	/* Get status of Entry : published / unpublished */
	function publicity($table_name, $field_prefix, $ID)
	{
		$CI =& get_instance();
		$query = $CI->db->get_where($table_name, array($field_prefix . '_ID' => $ID));
		if ($query->num_rows() > 0)
		{
		   $row = $query->row_array(); 
		
		   switch ($row[$field_prefix . '_published'])
		   {
		   		case '1' :
		   			return 'active';
		   			break;

		   		case '0' :
		   			return '<span style="color:#f00">inactive</span>';
		   			break;

		   }
		} 
	}
	
	function publishItems($table, $prKey, $field, $items_serialized, $action = 'toggle')
	{
		$CI =& get_instance();
		$items = explode("&", $items_serialized);
		if 		($action == "publish") 	 $publ = 1;
		else if ($action == "unpublish") $publ = 0;
		
		$error = "";
		foreach ($items as $id)
		{
			if ($action == "toggle")
			{
				$query = $CI->db->query("SELECT " . $field . " FROM " . $table . " WHERE " . $prKey . "=?", array($id));
				if ($query->num_rows() > 0)
				{
   					$row = $query->row_array(); 
					$publ = $row[$field];
					$action = ($publ == 1) ? "unpublish" : "publish";
					$publ = ($publ == 1) ? 0 : 1;
				} 
			}
	
			if ($field == 'trash')
			{
				$publ = 1;
			}
			
			$data = array($field => $publ);
			$CI->db->where($prKey, $id);	
			$res = $CI->db->update($table, $data);	
			
			if (is_null($res))
			{
				$error = true;
			}
		}
		
		$action = ($action == 'publish') ? 1 : 0;
		
		return array("error" => $error, "action" => $action);
	}
	
	function uploadMedia($userfile, $path, $id = '', $change_name = true)
	{	
		/*$max_image_width	= 401;
		$max_image_height	= 1001;
		$max_image_size		= 64 * 1024 * 4;*/
		$valid_types 		=  array("gif", "jpg", "png", "jpeg", "flv", "mp3", "xml");
		
		if (isset($userfile)) 
		{
			if (is_uploaded_file($userfile['tmp_name'])) 
			{
				$name = $userfile['name'];
				$filename = $userfile['tmp_name'];
				$ext = substr($userfile['name'], 1 + strrpos($userfile['name'], "."));
				$ext = strtolower($ext);
				if (!in_array($ext, $valid_types)) 
				{
					$res['error'] = "Error: Invalid file type.";
				}
				else 
				{		
					$res = array();
					$name = ($change_name) ? $id . "_" . $name : $name;
					$res['name'] = $name;
	      			@mkdir($path);
	        		$path = $path . $name;
					copy($filename, $path);
					//echo $filename . " " . $path;
					if (!in_array($ext, array("flv", "mp3")))
					{
						$res['attr'] = getimagesize($path);	
					}
					
					$res['filesize'] = filesize($path);
					$res['mimetype'] = 'jpeg';
				}
			} 
			else 
			{
				$res['error'] =	"Error: empty file.";
			}
			return $res;								
		}	
	}	
	
	function array_remove_element($arr, $element)
	{
		foreach($arr as $key => $value)
		{
			if($value == $element) 
			{
				unset($arr[$key]);
				return $arr;
			}
		}			
	}	

	function fromNumber2Letter($number)
	{
		$letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
		
		return $letters[$number];
	}
	
}

?>